package ytdp.careagle.com.ytdp.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;

import butterknife.ButterKnife;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.app.Operation;
import ytdp.careagle.com.ytdp.callback.MyDialogCallback;
import ytdp.careagle.com.ytdp.callback.MyDialogCallback2;
import ytdp.careagle.com.ytdp.dialog.CustomProgress;
import ytdp.careagle.com.ytdp.utils.MyToast;

public abstract class BaseActivity extends AppCompatActivity {

    private Toolbar toolbar;

    /**
     * 当前Activity的弱引用，防止内存泄露
     **/
    private WeakReference<Activity> context = null;

    /**
     * 共通操作
     **/
    private Operation mBaseOperation = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isFull()) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        if (isSteep() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            //设置状态栏透明
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
           /* Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);*/
        }
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mBaseOperation = new Operation(this);
        //将当前Activity压入栈
        context = new WeakReference<Activity>(this);
        APP.getInstance().getActivityList().add(this);
        initView();
        initView(savedInstanceState);
        View childAt = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        childAt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                APP.getInstance().resetCountDown();
                APP.getInstance().cancelCamera();
            }
        });

    }

    public void initView(Bundle savedInstanceState) {

    }

    /**
     * 获取共通操作机能
     */
    public Operation getOperation() {
        return this.mBaseOperation;
    }

    protected WeakReference<Activity> getWContext() {
        return context;
    }

    //设置全屏显示
    protected boolean isFull() {
        return false;
    }

    //设置沉浸式
    protected boolean isSteep() {
        return false;
    }

    protected void initToolbar(String title) {
        toolbar = (Toolbar) findViewById(R.id.base_toolbar);
        if (toolbar == null) {
            return;
        }
        TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.tv_toolbar_title);
        toolbarTitle.setText(title);

        ImageView ivBack = (ImageView) toolbar.findViewById(R.id.iv_toolbar_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    public Toolbar getToolbar() {
        return toolbar;
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void finish() {
        super.finish();
//		if (android.os.Build.VERSION.SDK_INT >= 5) {
//			overridePendingTransition(
//					R.anim.translate_between_interface_left_in,
//					R.anim.translate_between_interface_right_out);
//		}
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
//		if (android.os.Build.VERSION.SDK_INT >= 5) {
//			overridePendingTransition(
//					R.anim.zoomin,
//					R.anim.zoomout);
//		}
    }

    // 获取布局id
    public abstract int getLayoutId();


    /**
     * 初始化视图
     */
    public abstract void initView();


    /**
     * 当ui元素都不可见时，如切换到另一个app，进行资源释放操作
     */
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        switch (level) {
            case TRIM_MEMORY_UI_HIDDEN:
                // 进行资源释放操作
                break;
        }
    }

    public void showMessage(String str) {
        if (!TextUtils.isEmpty(str)) {
            MyToast.toast(str);
        }
    }

    private CustomProgress progressDialog;

    public void showProgress(int messageResId) {
        showProgress(getString(messageResId));
    }

    public void showProgress(String message) {
        showProgress(message, false);
    }

    public void showProgress(String message, boolean cancelable) {
        if (progressDialog != null && progressDialog.isShowing()) {
            return;
        }
        progressDialog = CustomProgress.show(this, message, cancelable, null);
    }

    public void hideProgress() {
        if (progressDialog != null) {
            try {
                progressDialog.dismiss();
                progressDialog = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void showMyDialog(String msg, final MyDialogCallback callback) {
        showMyDialog("提示", "msg", callback);
    }

    public void showMyDialog(String title, String msg, final MyDialogCallback callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                callback.onClickOk(dialogInterface, i);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.create().show();
    }

    public void showMyDialog2(String msg, final MyDialogCallback2 callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                callback.onClickOk(dialogInterface, i);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                callback.onCancel(dialogInterface, i);
                dialogInterface.dismiss();
            }
        });
        builder.create().show();
    }

    public void showMyDialog2(String msg, String content, final MyDialogCallback2 callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage(msg);
        builder.setMessage(content);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                callback.onClickOk(dialogInterface, i);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                callback.onCancel(dialogInterface, i);
                dialogInterface.dismiss();
            }
        });
        builder.create().show();
    }

    public void showCancelDialog(String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage(msg);
        builder.create().show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        APP.getInstance().getActivityList().remove(this);
    }
}
